package org.goyo.main.web.controller.storage;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.goyo.common.baseBean.AjaxResult;
import org.goyo.common.baseBean.Constants;
import org.goyo.common.utils.DateUtils;
import org.goyo.common.utils.JSONUtil;
import org.goyo.common.utils.Pager;
import org.goyo.common.utils.ToolUtils;
import org.goyo.main.domain.entity.pur.PurOrder;
import org.goyo.main.domain.entity.storage.GroupMaterial;
import org.goyo.main.domain.entity.storage.GroupMaterialEntry;
import org.goyo.main.domain.entity.system.SysUser;
import org.goyo.main.services.storage.IGroupMaterialService;
import org.goyo.main.web.controller.BaseController;
import org.goyo.main.web.controller.system.SessionUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
/**
 * 销售出库
 * 
 * @author chenchuan
 * 2014年11月12日
 */
@Controller
@RequestMapping("/groupMaterial")
public class GroupMaterialController extends BaseController  {
protected Logger logger = LoggerFactory.getLogger(this.getClass());
	
	@Resource
	private IGroupMaterialService groupMaterialService;
	
	/**
	 * 列表界面
	 * 
	 * @author chenchuan
	 * 2014年11月9日
	 */
	@RequestMapping("/toGroupMaterialList")
	public String toGroupMaterialList(){
		logger.info("----------------------");
		return "/storage/GroupMaterialList";
	}
	
	/**
	 * 保存或者更新
	 * 
	 * @author chenchuan
	 * 2014年11月9日
	 */
	@RequestMapping("/saveOrUpdateGroupMaterial")
	@ResponseBody
	public AjaxResult saveOrUpdateGroupMaterial(HttpServletRequest request){
		
		AjaxResult result = null;
		try{
			String form = request.getParameter("formData");
			String delData = request.getParameter("delData");
			GroupMaterial groupMaterial = JSONUtil.json2Obj(form, GroupMaterial.class);
			Object[] delUuids = null;
			if(delData != null &&!"null".equals(delData) &&!"undefined".equals(delData)){
				JSONArray delArray = JSONArray.fromObject(delData);
				List<GroupMaterialEntry> delEntry = JSONArray.toList(delArray, GroupMaterialEntry.class);
				delUuids = ToolUtils.getFiles(delEntry,"uuid").toArray();
			}
			GroupMaterial info = groupMaterialService.saveOrUpdateInfo(groupMaterial, delUuids,getCurrentUser());
			result = result.resultSuccess("保存成功", info);
		}catch(Exception e){
			result = AjaxResult.resultFail("保存失败", null);
			logger.error(e.getMessage(),e);
		}
		return result;
	}
	
	/**
	 * 提交数据
	 * 
	 * @author chenchuan
	 * 2014年11月16日
	 */
	@RequestMapping("/submitGroupMaterial")
	@ResponseBody
	public AjaxResult submitGroupMaterial(HttpServletRequest request){
		AjaxResult result = null;
		Object[] uuids = null;
		
		String rows = request.getParameter("rows");
		try{
			if(rows != null && !"".equals(rows)){
				JSONArray jsonarray = JSONArray.fromObject(rows);
				List<GroupMaterial> list = (List<GroupMaterial>)JSONArray.toCollection(jsonarray, GroupMaterial.class);
				uuids = ToolUtils.getFiles(list, "uuid").toArray();
			}else{
				String uuid = request.getParameter("uuid");
				uuids = new Object[]{uuid};
			}
			GroupMaterial info  = groupMaterialService.submitByUuids(uuids,getCurrentUser());
			result = AjaxResult.resultSuccess("提交成功", info);
		}catch(Exception e){
			result = AjaxResult.resultFail("提交失败", null);
			logger.error(e.getMessage(),e);
		}
		return result;
	}
	
	/**
	 * 审核数据
	 * 
	 * @author chenchuan
	 * 2014年11月16日
	 */
	@RequestMapping("/auditGroupMaterial")
	@ResponseBody
	public AjaxResult auditGroupMaterial(HttpServletRequest request){
		AjaxResult result = null;
		Object[] uuids = null;
		
		String rows = request.getParameter("rows");
		try{
			if(rows != null && !"".equals(rows)){
				JSONArray jsonarray = JSONArray.fromObject(rows);
				List<GroupMaterial> list = (List<GroupMaterial>)JSONArray.toCollection(jsonarray, GroupMaterial.class);
				uuids = ToolUtils.getFiles(list, "uuid").toArray();
			}else{
				String uuid = request.getParameter("uuid");
				uuids = new Object[]{uuid};
			}
			GroupMaterial info = groupMaterialService.auditByUuids(uuids,getCurrentUser());
			result = AjaxResult.resultSuccess("审核成功", info);
		}catch(Exception e){
			result = AjaxResult.resultFail("审核失败", null);
			logger.error(e.getMessage(),e);
		}
		return result;
	}
	
	/**
	 * 反审核数据
	 * 
	 * @author chenchuan
	 * 2014年11月16日
	 */
	@RequestMapping("/unAuditGroupMaterial")
	@ResponseBody
	public AjaxResult unAuditGroupMaterial(HttpServletRequest request){
		AjaxResult result = null;
		Object[] uuids = null;
		
		String rows = request.getParameter("rows");
		try{
			if(rows != null && !"".equals(rows)){
				JSONArray jsonarray = JSONArray.fromObject(rows);
				List<GroupMaterial> list = (List<GroupMaterial>)JSONArray.toCollection(jsonarray, GroupMaterial.class);
				uuids = ToolUtils.getFiles(list, "uuid").toArray();
			}else{
				String uuid = request.getParameter("uuid");
				uuids = new Object[]{uuid};
			}
			GroupMaterial info = groupMaterialService.unAuditByUuids(uuids,getCurrentUser());
			result = AjaxResult.resultSuccess("反审核成功", info);
		}catch(Exception e){
			result = AjaxResult.resultFail("反审核失败", null);
			logger.error(e.getMessage(),e);
		}
		return result;
	}
	
	
	/**
	 * 删除
	 * 
	 * @author chenchuan
	 * 2014年11月9日
	 */
	@RequestMapping("/delGroupMaterial")
	@ResponseBody
	public AjaxResult delGroupMaterial(HttpServletRequest request){
		AjaxResult result = null;
		String rows = request.getParameter("rows");
		try{
			JSONArray jsonarray = JSONArray.fromObject(rows);
			List<GroupMaterial> list = (List<GroupMaterial>)JSONArray.toCollection(jsonarray, GroupMaterial.class);
			groupMaterialService.delByUuids(ToolUtils.getFiles(list,"uuid").toArray());
			result = AjaxResult.resultSuccess("删除成功", null);
		}catch(Exception e){
			result = AjaxResult.resultFail("删除失败", null);
			logger.error(e.getMessage(),e);
		}
		return result;
	}
	
	/**
	 * 查看
	 * 
	 * @author chenchuan
	 * 2014年11月9日
	 */
	@RequestMapping("/viewGroupMaterial")
	public String viewGroupMaterial(HttpServletRequest request,Model model){
		String uuid = request.getParameter("uuid");
		GroupMaterial info = null;
		try {
			info = groupMaterialService.findByUuuid(uuid);
		} catch (Exception e) {
			logger.error(e.getMessage(),e);
		}
		model.addAttribute("info", JSONUtil.obj2Json(info));
		model.addAttribute("editStatus", Constants.VIEW);
		return "/storage/GroupMaterialEdit";
	}
	
	/**
	 * 修改
	 * 
	 * @author chenchuan
	 * 2014年11月9日
	 */
	@RequestMapping("/alterGroupMaterial")
	public String alterGroupMaterial(HttpServletRequest request,Model model){
		String uuid = request.getParameter("uuid");
		GroupMaterial info = null;
		try {
			info = groupMaterialService.findByUuuid(uuid);
		} catch (Exception e) {
			logger.error(e.getMessage(),e);
		}
		model.addAttribute("info", JSONUtil.obj2Json(info));
		model.addAttribute("editStatus", Constants.EDIT);
		return "/storage/GroupMaterialEdit";
	}
	
	/**
	 * 添加
	 * 
	 * @author chenchuan
	 * 2014年11月9日
	 */
	@RequestMapping("/addGroupMaterial")
	public String addGroupMaterial(HttpServletRequest request,Model model){
		GroupMaterial info = new GroupMaterial();
		//获取登陆用户
		SysUser user = (SysUser) SessionUtil.getAttribute("user");
		info.setCreateDate(DateUtils.getCurrentDate());
		info.setCreateUser(user);
		info.setAlterDate(DateUtils.getCurrentDate());
		info.setAlterUser(user);
		info.setStatus(Constants.NEWSTATUS);
		info.setNumber(getBillNumber(GroupMaterial.BIZTYPE));
		info.setDepGroup(user.getDepGroup());
		model.addAttribute("info",JSONUtil.obj2Json(info));
		model.addAttribute("editStatus", Constants.NEW);
		return "/storage/GroupMaterialEdit";
	}
	
	
	@RequestMapping("/listEntryData")
	@ResponseBody
	public String listEntryData(HttpServletRequest request){
		JSONObject data = null;
		request.getParameterMap();
		String uuid = request.getParameter("uuid");
		if(uuid == null || "".equals(uuid))
			return null;
		try {
			List<GroupMaterialEntry> entrys = groupMaterialService.findByParentId(uuid);
			Map<String,Object> map = new HashMap<String,Object>();
			map.put("Rows", entrys);
			data = JSONObject.fromObject(map);
		} catch (Exception e) {
			logger.error(e.getMessage(),e);
		}
		return data.toString();

	}
	
	/**
	 * list列表数据加载
	 * 
	 * @author chenchuan
	 * 2014年11月9日
	 */
	@RequestMapping("/listData")
	@ResponseBody
	public String ListData(HttpServletRequest request){
		JSONObject data = null;
		//分页条件
		int page = Integer.parseInt(request.getParameter("page"));
		int pageSize = Integer.parseInt(request.getParameter("pagesize"));
		String sortname = request.getParameter("sortname");
		String sortorder = request.getParameter("sortorder");
		
		String where = request.getParameter("where"); 
		//分页器
		Pager pager = Pager.convert(page, pageSize, sortname, sortorder);
		try {
			pager = groupMaterialService.findByCondition(where, pager);
			Map<String,Object> map = new HashMap<String,Object>();
			map.put("Rows", pager.getList());
			map.put("Total", pager.getCount());
			data = JSONObject.fromObject(map);
		}catch (Exception e) {
			logger.error(e.getMessage(),e);
		} 
		return data.toString();
	}
	

}
